<?php
include("mysqlcon.php");
mysql_select_db($dbname,$conn);
mysql_query('SET NAMES utf8');

if($_GET["mode"] == 'search'){
	$error = "";
	$msg = "";
	$value = $_GET["value"];
	$customer_id = "";
	$customer_name = "";
	$customer_surname = "";
	$customer_address = "";
	$customer_email = "";
	$customer_tel = "";
	$customer_fax = "";
	
	$search_sql = "SELECT customer_id, customer_name, customer_surname, customer_address, customer_email, customer_tel, customer_fax, customer_group_name ".
				" FROM customer ".
				//" WHERE customer_name LIKE '%".$value."%'";
				" WHERE id = $value ";
	$search_result = mysql_query($search_sql);
	$total = mysql_num_rows ( $search_result );
	while($rs1 = mysql_fetch_array($search_result))
	{

		$customer_id = $rs1[customer_id];
		$customer_name = $rs1[customer_name];
		$customer_surname = $rs1[customer_surname];
		$customer_address = $rs1[customer_address];
		$customer_email = $rs1[customer_email];
		$customer_tel = $rs1[customer_tel];
		$customer_fax = $rs1[customer_fax];
		$customer_group_name = $rs1[customer_group_name];
		
	}
	echo "{";
	echo				"error: '" . $error . "',\n";
	echo				"customer_id: '" . $customer_id . "',\n";
	echo				"customer_name: '" . $customer_name . "',\n";
	echo				"customer_surname: '" . $customer_surname . "',\n";
	echo				"customer_address: '" . $customer_address . "',\n";
	echo				"customer_email: '" . $customer_email . "',\n";
	echo				"customer_tel: '" . $customer_tel . "',\n";
	echo				"customer_fax: '" . $customer_fax . "',\n";
	echo				"customer_group_name: '" . $customer_group_name . "',\n";
	echo				"total: '" . $total . "',\n";
	echo				"msg: '" . $msg . "'\n";
	echo "}";
	
}else{
	
	$sql = "SELECT * FROM customer ";
	$result = mysql_query($sql);
	$items = Array();
	while($rs = mysql_fetch_array($result))
	{
		$id =  $rs[id];
		$customer_id =  $rs[customer_id];
		$customer_name =  $rs[customer_name];
		$customer_surname = $rs[customer_surname];
		$cus = $customer_name. " " .$customer_surname ." ". $customer_id;
		$items[$cus] = $id;
	}
	      
	$q = strtolower($_GET["q"]);
	if (!$q) return;
	
	
	foreach ($items as $key=>$value) {
		if (strpos(strtolower($key), $q) !== false) {
			echo "$key|$value\n";
		}
	}
}
?>